// pages/category/index.js
import {request} from '../../utils/request.js';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        cateList:[],
        rightList:[],
        current:0,
        scrollTop:0
    },
   
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

       


        let cates = wx.getStorageSync("cates");
        let cateList = cates.data;
        const time =cates.time;//设置缓存的时间
        const now = Date.now();

        console.log(now - time);
        //24*60*60*1000 一天
        // 10000  10秒
        if(cates && ((now - time) <  10000) ){
            console.log("使用缓存....");
            this.init(cateList)
        }else{
            console.log("不存在缓存，或者超期");
            this.getCateList();
        }
       
    },

    //获取分类数据
    getCateList:function(){
        request({url:'/categories'}).then((cateList)=>{
            wx.setStorageSync("cates",{
                time:Date.now(),
                data:cateList
            });
            this.init(cateList)
        });
    }, 
    init:function(cateList){
        let rightList = cateList[0].children; //默认右侧显示第一个子分类
        this.setData({cateList,rightList});
    },

    handleItemTap(e){
        let current = e.target.dataset.item;
        let rightList= this.data.cateList[current].children;
        this.setData({current,rightList})
        this.setData({
            scrollTop:0
        })
    }
})